//
//  ZJSettingCell.m
//  Lottery
//
//  Created by ZJ on 2018/8/16.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "ZJSettingCell.h"

static NSString * const cellID = @"setting_cell";

@interface ZJSettingCell ()

@property (nonatomic, strong) NSDictionary *item;

@end

@implementation ZJSettingCell

+ (instancetype)settingCellWithTableView:(UITableView *)tableView item:(NSDictionary *)item {
    // 缓存池找
    ZJSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[ZJSettingCell alloc] initWithStyle:[self loadCellStyleWIthItem:item] reuseIdentifier:cellID];
    }
    
    cell.item = item;
    
    return cell;
}

- (void)setItem:(NSDictionary *)item {
    _item = item;
    
    // 把数据放到 cell 上
    
    // 赋值
    NSString *imageName = item[@"icon"];
    if (imageName && imageName.length > 0) {
        self.imageView.image = [UIImage imageNamed:imageName]; // ---设置图片
    }
    
    self.textLabel.text = item[@"title"]; // ---设置标题
    self.detailTextLabel.text = item[@"subTitle"]; // ---设置子标题
    
    // 判断是否需要设置成红色
    if ([item[@"isRed"] boolValue] && item[@"isRed"]) {
        self.detailTextLabel.textColor = [UIColor redColor]; // ---设置子标题颜色
    }
    
    // 根据字符创生成对象======
    NSString *accessoryType = item[@"accessoryType"]; // 获取到 UISwith的字符串   @"UISwitch"
    Class cls = NSClassFromString(accessoryType); // 获取 UISwitch的类型 UISwitch
    UIView *obj = [[cls alloc] init]; // 获取 UISwitch类型的对象
    
    // 判断 obj 真实的类型
    if ([obj isKindOfClass:[UIImageView class]]) {
        // 设置 frame,图片
        UIImageView *imageView = (UIImageView *)obj;
        imageView.image = [UIImage imageNamed:item[@"accessoryContent"]];
        [imageView sizeToFit];
    }
    
    self.accessoryView = obj; // ---设置accessoryView
}

// 根据传入的 cell 的类型 返回需要创建的 cell 的类型
+ (UITableViewCellStyle)loadCellStyleWIthItem:(NSDictionary *)item {
    NSString *cellType = item[@"cellType"];
    
    if ([cellType isEqualToString:@"UITableViewCellStyleSubtitle"]) {
        return UITableViewCellStyleSubtitle;
    } else if ([cellType isEqualToString:@"UITableViewCellStyleValue1"]) {
        return UITableViewCellStyleValue1;
    } else if ([cellType isEqualToString:@"UITableViewCellStyleValue2"]) {
        return UITableViewCellStyleValue2;
    }
    
    return UITableViewCellStyleDefault;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
